﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.IO;

namespace Idlesoft.ShellAnywhereLanguage
{
	public class Point : IXmlSerializable
	{
		#region ctors
		public Point() { }
		public Point(int? x, int? y) { X = x; Y = y; }
		public Point(string point) { FromString(point); }
		#endregion ctors

		#region props
		public int? X { get; set; }
		public int? Y { get; set; }
		#endregion props

		#region methods
		public XmlSchema GetSchema() { return null; }
		public void ReadXml(XmlReader reader)
		{
			reader.MoveToAttribute("Point");
			string point = reader.ReadContentAsString();
			FromString(point);
		}

		public void WriteXml(XmlWriter writer)
		{
			writer.WriteStartAttribute("Point");
			writer.WriteString(ToString());
			writer.WriteEndAttribute();
		}
		public override string ToString()
		{
			string x = (X != null) ? X.ToString() : "Auto";
			string y = (Y != null) ? Y.ToString() : "Auto";
			return "{" + x + "," + y + "}";
		}
		public void FromString(string point)
		{
			if (!string.IsNullOrEmpty(point))
			{
				Match match = Regex.Match(point, "{([A-Za-z0-9]+),([A-Za-z0-9]+)}");
				string x = match.Groups[1].ToString();
				string y = match.Groups[2].ToString();
				X = (!string.IsNullOrEmpty(x) && x != "Auto") ? Convert.ToInt32(x) : default(int?);
				Y = (!string.IsNullOrEmpty(y) && y != "Auto") ? Convert.ToInt32(y) : default(int?);
			}
		}
		#endregion methods
	}
	public abstract class Target : Param, ITarget
	{
		#region ctors
		public Target(string value) : base(value) { }
		#endregion ctors

		#region props
		[XmlIgnore]
		public override LexemeType LexemeType { get { return LexemeType.Target; } }
		#endregion props

		#region methods
		public override bool IsConstrainedBy<TItem>(TItem item)
		{
			return base.IsConstrainedBy<TItem>(item);
		}
		#endregion methods

		#region fields
		#endregion fields
	}
}
